use std::env;
use std::fs;
use std::io;
use std::process::Command;

fn download(url: &str, path: &str) -> io::Result<()> {
    if fs::exists(path)? {
        return Ok(());
    }

    Command::new("wget")
        .arg("-O")
        .arg(path)
        .arg(url)
        .status()
        .map(|status| assert!(status.success()))
}

fn main() -> io::Result<()> {
    let out_dir = env::var("OUT_DIR").unwrap();
    assert!(!out_dir.is_empty());

    download(
        "https://nugine.xyz/__secret_data/region.json",
        &format!("{out_dir}/region.json"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/wadegiles_phrases.txt",
        &format!("{out_dir}/wadegiles_phrases.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/wadegiles_chars.txt",
        &format!("{out_dir}/wadegiles_chars.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/s2t_phrases.txt",
        &format!("{out_dir}/s2t_phrases.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/s2t_chars.txt",
        &format!("{out_dir}/s2t_chars.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/t2s_phrases.txt",
        &format!("{out_dir}/t2s_phrases.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/t2s_chars.txt",
        &format!("{out_dir}/t2s_chars.txt"),
    )?;

    download(
        "https://nugine.xyz/__secret_data/lunar11.txt",
        &format!("{out_dir}/lunar11.txt"),
    )?;

    Ok(())
}
